package com.example.administrator.spritelgs.view;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.administrator.spritelgs.R;
import com.example.administrator.spritelgs.util.AnimUtil;

/**
 * Created by Administrator on 2017/6/30.
 */

public class ShopView extends BaseView{
    private ImageView iv_chongzhi;
    private ImageView iv_figure;
    private ImageView iv_goumai;
    private ImageView iv_kuochong;
    private ImageView iv_buchongtili;
    private ImageView iv_buchongjingli;

    private FrameLayout fl_btns;
    private AnimationSet animSetFigure;

    private ChongZhiView chongZhiView;

    public ShopView(Context context){
        super(context);
        setContentView(R.layout.view_shop);

        initUI();
        fugureAnimation();
    }

    private void fugureAnimation(){
        animSetFigure = new AnimationSet(false);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1f, 1.0f, 0.95f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);
        scaleAnimation.setDuration(800);
        scaleAnimation.setRepeatCount(Animation.INFINITE);//无限重复
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        animSetFigure.addAnimation(scaleAnimation);
        //iv_figure.startAnimation(scaleAnimation);

        final TranslateAnimation tranX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
        tranX.setDuration(400);
        animSetFigure.addAnimation(tranX);
        //iv_figure.startAnimation(animSetFigure);
    }

    public void show(){
        TranslateAnimation tranY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF,-1f,Animation.RELATIVE_TO_SELF,0f);
        tranY.setDuration(400);
        fl_btns.startAnimation(tranY);
        //iv_figure.startAnimation(animSetFigure);
        tranY.setAnimationListener(new Animation.AnimationListener(){

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_figure.setVisibility(View.VISIBLE);
                iv_figure.startAnimation(animSetFigure);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void hide(Animation.AnimationListener listener){
        TranslateAnimation tranX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f,
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
        tranX.setDuration(400);
        TranslateAnimation tranY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1f);
        tranY.setDuration(400);
        fl_btns.startAnimation(tranY);
        iv_figure.startAnimation(tranX);
        iv_figure.setVisibility(View.INVISIBLE);
        if(listener!=null)
            tranX.setAnimationListener(listener);
    }

    @Override
    protected void initUI(){
        iv_chongzhi = (ImageView) findViewById(R.id.iv_chongzhi);
        AnimUtil.addOnTouchListener(iv_chongzhi, 0.5f, 0.5f, new AnimUtil.AnimListener() {
            @Override
            public void down(View v) {

            }

            @Override
            public void move(View v) {

            }

            @Override
            public void up(View v) {
                chongZhiView.show();
            }
        });
        iv_goumai = (ImageView) findViewById(R.id.iv_goumai);
        AnimUtil.addOnTouchListener(iv_goumai, 0.5f, 0.5f, null);
        iv_kuochong = (ImageView) findViewById(R.id.iv_kuochong);
        AnimUtil.addOnTouchListener(iv_kuochong, 0.5f, 0.5f, null);
        iv_buchongtili = (ImageView) findViewById(R.id.iv_buchongtili);
        AnimUtil.addOnTouchListener(iv_buchongtili, 0.5f, 0.5f, null);
        iv_buchongjingli = (ImageView) findViewById(R.id.iv_buchongjingli);
        AnimUtil.addOnTouchListener(iv_buchongjingli, 0.5f, 0.5f, null);
        iv_figure = (ImageView) findViewById(R.id.iv_figure);
        iv_figure.setVisibility(View.INVISIBLE);
        fl_btns = (FrameLayout)findViewById(R.id.fl_btns);

        chongZhiView = new ChongZhiView(context);
    }

}
